package com.scoy.honeymei.activity.film;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.BannerBean;
import com.scoy.honeymei.bean.CinemaBean;
import com.scoy.honeymei.databinding.ActivityFilmAdsBinding;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.install.GlideImageLoader;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:      com.scoy.honeymei.activity.film
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2021/1/8/0008$ 17:24$
 * UpdateRemark:   更新内容
 */
public class FilmAdsActivity extends BaseActivity {

    private ActivityFilmAdsBinding binding;
    private FilmAdsActivity mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFilmAdsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = FilmAdsActivity.this;

        getData();

        initNormal();
    }

    @Override
    public void initNormal() {

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorTrans);

        binding.afaTimeTv.setOnClickListener(v -> {
            //0=未观看,1=已观看
            SPHelper.applyInt(MeConstant.STATE_SHOW, 1);
            startActivity(new Intent(mContext, CinemaListActivity.class));
            onBackPressed();
        });

    }

    private void getData() {

        HttpParams params = new HttpParams();
        params.put("user_id", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(mContext, MyUrl.CINAME_GUIDE_SHOW, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<String> mlist = new Gson().fromJson(o, new TypeToken<List<String>>() {
                }.getType());

                if (mlist == null || mlist.size() == 0) {
                    //0=未观看,1=已观看
                    SPHelper.applyInt(MeConstant.STATE_SHOW, 1);
                    startActivity(new Intent(mContext, CinemaListActivity.class));
                    onBackPressed();
                } else if (mlist.size() == 1) {
                    binding.afaTimeTv.setVisibility(View.VISIBLE);
                } else {
                    binding.afaTimeTv.setVisibility(View.GONE);
                }
                initBanner(binding.afaBanner, mlist);
            }
        });

    }


    /**
     * banner图
     */
    public void initBanner(Banner banner, List<String> imglist) {

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(imglist);
        banner.setBannerAnimation(Transformer.Default);
        banner.isAutoPlay(false);
        banner.setOnBannerListener(position -> {


        });

        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                binding.afaTimeTv.setVisibility(imglist.size() - 1 == position ? View.VISIBLE : View.GONE);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        banner.start();
    }

}
